# $Id

package WidgetSetExtension::CMS;
use strict;
use base qw( MT::App );

use MT::Template;

sub start_create_widget {
    my $app   = shift;
    my $perms = $app->permissions
      or return $app->error( $app->translate("No permissions") );
    return $app->error( $app->translate("Permission denied.") )
      unless $perms->can_edit_templates;

    my $plugin = MT::Plugin::WidgetSetExtension->instance;
    my $q      = $app->param;

    my $blog_id = $q->param('blog_id') || 0;
    my $name = $plugin->translate('Blank Widget');
    my $unique_name;
    my $i = 1;
    do {
        $unique_name = $name . ' ' . $i++;
      } while (
        MT::Template->count(
            {
                name    => $unique_name,
                type    => 'widget',
                blog_id => $blog_id ? [ $blog_id, 0 ] : 0,
            }
        )
      );

    $i--;
    my $text = qq{
<div class="widget-blank-$i widget">
    <h3 class="widget-header">
        $unique_name
    </h3>
    <div class="widget-content">
        
    </div>
</div>
};

    my $tmpl = $plugin->load_tmpl('create_widget.tmpl');
    $app->build_page(
        $tmpl,
        {
            blog_id     => $blog_id,
            id          => $q->param('id'),
            name        => $unique_name,
            text        => $text,
            return_args => $q->param('return_args'),
        }
    );
}

sub create_widget {
    my $app = shift;
    $app->validate_magic() or return;
    my $perms = $app->permissions
      or return $app->error( $app->translate("No permissions") );
    return $app->error( $app->translate("Permission denied.") )
      unless $perms->can_edit_templates;

    my $q = $app->param;

    my $widget = MT::Template->new;
    $widget->name( $q->param('name') );
    $widget->text( $q->param('text') );
    $widget->type('widget');
    $widget->blog_id( $q->param('blog_id') || 0 );
    $widget->save
      or return $app->error( $app->translate("Failed to save a widget.") );

    if ( $q->param('widget_mode') ) {
        my $widgetset = MT::Template->load( $q->param('id') );
        my @modules = split( ',', $widgetset->modulesets );
        push @modules, $widget->id;
        $widgetset->modulesets( join( ',', @modules ) );
        $widgetset->save
          or return $app->error(
            $app->translate("Failed to update a widgetset.") );
    }

    my $plugin = MT::Plugin::WidgetSetExtension->instance;
    my $tmpl   = $plugin->load_tmpl('finish_create_widget.tmpl');
    $app->build_page( $tmpl, { return_args => $q->param('return_args') } );
}

sub template_source {
    my ( $cb, $app, $tmpl ) = @_;
    my $pattern = qq{<div id="edit-form">};
    my $append  = qq{
    <p><a href="javascript:void(0)" onclick="return openDialog(false, 'start_create_widget', 'blog_id=<mt:var name="blog_id" escape="url">&amp;id=<mt:var name="id" escape="url">&amp;return_args=<mt:var name="return_args" escape="url">')" class="icon-left icon-create"><__trans phrase="Create widget template"></a></p>
};
    $$tmpl =~ s/$pattern/$pattern$append/;
}

1;
